package com.dinzeer.slashblade_useful_addon.mixin;

import mods.flammpfeil.slashblade.recipe.SlashBladeShapedRecipe;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Map;

@Mixin(SlashBladeShapedRecipe.class)
public class ShapedRecipeMixin {
    @Inject(method = "updateEnchantment", cancellable = true, at = @At("HEAD"), remap = false)
    private void updateEnchantment(ItemStack result, ItemStack ingredient, CallbackInfo ci) {
        Map<Enchantment, Integer> newItemEnchants = result.getAllEnchantments();
        Map<Enchantment, Integer> oldItemEnchants = ingredient.getAllEnchantments();

        // 直接合并所有附魔（无任何限制）
        for (Map.Entry<Enchantment, Integer> entry : oldItemEnchants.entrySet()) {
            Enchantment enchantment = entry.getKey();
            int oldLevel = entry.getValue();
            int newLevel = Math.max(oldLevel, newItemEnchants.getOrDefault(enchantment, 0));


            newItemEnchants.put(enchantment, newLevel);
        }

        EnchantmentHelper.setEnchantments(newItemEnchants, result);
        ci.cancel(); // 完全覆盖原版逻辑
    }


}
